package com.tz.farmer.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.enums.ErrorCodeEnum;
import com.tz.farmer.service.CockpitService;
import com.tz.farmer.util.ResultExM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @description: 驾驶舱
 * @name BusinessController
 * @author: xhj
 * @date: 2021/12/6
 * @version V1.0
 */
@RestController
@RequestMapping("cockpit")
@Api(value = "驾驶舱", tags = "驾驶舱控制器")
public class CockpitController {

    private static Logger logger = LoggerFactory.getLogger(CockpitController.class);


    @Resource
    CockpitService cockpitService;

    @Autowired
    RestTemplate restTemplate;

    /**
     * @description:  核心指标
     * @name businessInfo
     * @author: xhj
     * @date: 2021/12/6 11:35
     * @version V1.0
     */
    @ApiOperation("核心指标")
    @GetMapping("coreIndex")

    public ResultEx getCoreIndex() {
        try {
            return cockpitService.getCoreIndex();
        }catch (Exception e){
            logger.error("船港通数据获取失败",e);
            return ResultEx.builder().makeFailedResult("船港通数据获取失败");

        }
    }


    /**
     * @description:  食安科普
     * @name businessInfo
     * @author: xhj
     * @date: 2021/12/6 11:35
     * @version V1.0
     */
    @ApiOperation("食安科普")
    @GetMapping("popularInfo")

    public ResultEx getPopularInfo() {
        try {
            return cockpitService.getPopularInfo();
        }catch (Exception e){
            logger.error("食安科普数据获取失败",e);
            return ResultEx.builder().makeFailedResult("食安科普数据获取失败");

        }
    }


    /**
     * 查询阳光渔场统计数值
     *
     * @return
     */
    @GetMapping("/screenThingStatistics")
    @ApiOperation("查询阳光渔场统计数值")

    public ResultEx getScreenThingStatistics() {

        String url = "http://61.175.223.173:34080/screenThing";
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, "");
        String body = responseEntity.getBody();
        if (body != null) {
            JSONObject data = (JSONObject) JSON.parse(body);
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED, data);
        }
        return ResultExM.builder().makeFailedResult();
    }

    /**
     * 查询阳光渔场展示列表
     *
     * @return
     */
    @GetMapping("/screenThingList")
    @ApiOperation("查询阳光渔场展示列表")

    public ResultEx getScreenThingList() {

        String url = "http://61.175.223.173:34080/screenThingList";
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, "");
        String body = responseEntity.getBody();
        if (body != null) {
            JSONObject data = (JSONObject) JSON.parse(body);
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED, data);
        }
        return ResultExM.builder().makeFailedResult();
    }

    /**
     * 查询阳光渔场养殖-农事操作展示列表
     *
     * @return
     */
    @GetMapping("/screenOperateList")
    @ApiOperation("查询阳光渔场养殖-农事操作展示列表")

    public ResultEx getScreenOperateList() {
        try {
            return cockpitService.getScreenOperateList();
        }catch (Exception e){
            logger.error("农事操作展示列表获取失败",e);
            return ResultEx.builder().makeFailedResult("农事操作展示列表获取失败");

        }
    }

    /**
     * 渔船列表
     *
     * @return
     */
    @GetMapping("/boatList")
    @ApiOperation("渔船列表")

    public ResultEx getBoatList() {
        String url = "http://61.175.223.173:34080/boatList";
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, "");
        String body = responseEntity.getBody();
        if (body != null) {
            JSONArray data = JSONArray.parseArray(body);
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED, data);
        }
        return ResultExM.builder().makeFailedResult();
    }

}
